import axios from 'axios'
import qs from 'qs'

export default {
    namespaced: true,
    actions: {
        getNotice: function ({commit}, params) {
            let {callback} = params;
            params.onget = ({data}) => {
                callback(data);
            }
            commit("getNotice", params);
        },
        getAllNotice: function ({commit}, params) {
            let {callback} = params;
            params.onget = ({data}) => {
                callback(data);
            }
            commit("getAllNotice", params);
        },
        addNotice: function ({commit}, params) {
            let {callback} = params;
            params.onget = ({data}) => {
                callback(data);
            }
            commit("addNotice", params);
        },
        updateNotice: function ({commit}, params) {
            let {callback} = params;
            params.onget = ({data}) => {
                callback(data);
            }
            commit("updateNotice", params);
        },
        deleteNotice: function ({commit}, params) {
            let {callback} = params;
            params.onget = ({data}) => {
                callback(data);
            }
            commit("deleteNotice", params);
        },
    },
    mutations: {
        getNotice: function (state, {id, onget}) {
            axios.get(`/admin/notice/${id}`).then(data => {
                onget(data);
            })
        },
        getAllNotice: function (state, {title, message, publisher, page, size, onget}) {
            axios.get(`/admin/notice`, {
                params: {
                    title, message, publisher, page, size
                }
            }).then(data => {
                onget(data);
            })
        },
        addNotice: function (state, {title, message, publisher, onget}) {
            axios.post(`/admin/notice`, qs.stringify({
                title, message, publisher
            })).then(data => {
                onget(data);
            })
        },
        updateNotice: function (state, {id, title, message, publisher, onget}) {
            axios.put(`/admin/notice`, qs.stringify({
                id, title, message, publisher
            })).then(data => {
                onget(data);
            })
        },
        deleteNotice: function (state, {id, onget}) {
            axios.delete(`/admin/notice`,qs.stringify({
                id
            })).then(data => {
                onget(data);
            })
        },
    }

}